package com.itboye.boyeshoping.fragment

import android.annotation.SuppressLint
import android.app.ActionBar
import android.content.Context
import android.graphics.Color
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.widget.LinearLayout
import com.itboye.boyeshoping.R
import com.itboye.boyeshoping.ViewUtil.HorzontalScrollTable
import com.itboye.boyeshoping.base.BaseFragment
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import com.itboye.boyeshoping.ViewUtil.magicIndicator.MagicIndicator
import com.itboye.boyeshoping.ViewUtil.magicIndicator.ViewPagerHelper
import com.itboye.boyeshoping.ViewUtil.magicIndicator.commonnavigator.CommonNavigator
import com.itboye.boyeshoping.ViewUtil.magicIndicator.commonnavigator.abs.CommonNavigatorAdapter
import com.itboye.boyeshoping.ViewUtil.magicIndicator.commonnavigator.abs.IPagerIndicator
import com.itboye.boyeshoping.ViewUtil.magicIndicator.commonnavigator.abs.IPagerTitleView
import com.itboye.boyeshoping.ViewUtil.magicIndicator.commonnavigator.indicators.LinePagerIndicator
import com.itboye.boyeshoping.ViewUtil.magicIndicator.commonnavigator.titles.ColorTransitionPagerTitleView
import com.itboye.boyeshoping.ViewUtil.magicIndicator.commonnavigator.titles.SimplePagerTitleView
import com.itboye.boyeshoping.adapter.ViewPagerFragmentsAdapter
import com.itboye.boyeshoping.inf.OnItemClickListenner


/**
 * Created by Administrator on 2018/6/27 0027.
 */
class CheapFragment : BaseFragment() {

    private lateinit var tableIndicator:MagicIndicator
    private lateinit var goodsVp: ViewPager
    private var array: List<String>? = null
    private var fragments :List<ViewPagerFragment>?=null
    private var mExamplePagerAdapter:ViewPagerFragmentsAdapter? = null
    private lateinit var frag:ViewPagerFragment
    override fun initView(): Int {
     return R.layout.fragment_baicai

    }

    override fun onMyActivityCreated() {
        goodsVp= activity!!.findViewById(R.id.goodsVp) as ViewPager

        array=ArrayList()
        fragments=ArrayList()
        (array as ArrayList<String>).add("一元购")
        (array as ArrayList<String>).add("9.9包邮")
        (array as ArrayList<String>).add("食品")
        (array as ArrayList<String>).add("女装")
        (array as ArrayList<String>).add("成人")
        (array as ArrayList<String>).add("儿童")
        (array as ArrayList<String>).add("妇女")

        val manager = activity!!.windowManager
        val outMetrics = DisplayMetrics()
        manager.defaultDisplay.getMetrics(outMetrics)

        for (i in (array as ArrayList<String>).indices) {

            (fragments as ArrayList<ViewPagerFragment>).add(ViewPagerFragment().newInstance(i,activity))
        }

        mExamplePagerAdapter= ViewPagerFragmentsAdapter(activity!!.supportFragmentManager,fragments)
        goodsVp.adapter=mExamplePagerAdapter
        initMagicIndicator3()
    }

    @SuppressLint("NewApi")
    private fun initMagicIndicator3() {
        val magicIndicator = activity!!.findViewById(R.id.tableIndicator) as MagicIndicator
        magicIndicator.setBackgroundColor(activity!!.getColor(R.color.maincolor))
        val commonNavigator = CommonNavigator(activity)
        commonNavigator.adapter = object : CommonNavigatorAdapter(){

            override fun getCount(): Int {
                return  array!!.size
            }

            override fun getTitleView(context: Context?, index: Int): IPagerTitleView? {

                val simplePagerTitleView :SimplePagerTitleView = ColorTransitionPagerTitleView(context)
                simplePagerTitleView.normalColor = Color.WHITE
                simplePagerTitleView.selectedColor = Color.WHITE
                simplePagerTitleView.text = array!!.get(index)
                simplePagerTitleView.setOnClickListener { goodsVp.currentItem = index }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context?): IPagerIndicator? {
                val linePagerIndicator = LinePagerIndicator(context)
                linePagerIndicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                linePagerIndicator.setColors(Color.WHITE)
                return linePagerIndicator

            }
        }
        magicIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(magicIndicator, goodsVp)
    }


}